package com.qfedu.service;

import com.qfedu.dao.SongListDao;
import com.qfedu.entity.ListSong;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.Song;
import com.qfedu.entity.SongList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.lang.model.element.VariableElement;
import java.util.ArrayList;
import java.util.List;

@Service
public class SongListServiceImpl implements SongListService {


    @Autowired
    private SongListDao songListDao;

    @Override
    public List<SongList> findAllSongList() {
        return songListDao.findAllSongList();
    }

    @Override
    public List<SongList> findSongStyleList(String style) {
        return songListDao.findSongStyleList(style);
    }

    @Override
    public List<ListSong> getListSongOfSongId(int songListId) {
        return songListDao.getListSongOfSongId(songListId);
    }

    @Override
    public List<Song> getSongOfId(int id) {
        return songListDao.getSongOfId(id);
    }

    @Override
    public List<Song> getSongOfSingerId(int singerId) {
        return songListDao.getSongOfSingerId(singerId);
    }

    @Override
    public List<Song> getSongOfSingerName(String name) {


        if (!StringUtils.isEmpty(name)){

            return songListDao.getSongOfSingerName("%"+name+"%");

        }
        return new ArrayList<>();
    }

    @Override
    public List<SongList> getSongListOfLikeTitle(String title) {
        if (!StringUtils.isEmpty(title)){

            return songListDao.getSongListOfLikeTitle("%"+title+"%");
        }
        return new ArrayList<>();
    }

    /**
     * 更新歌曲信息
     * @param song
     * @return
     */
    @Override
    public Boolean updateSongMsg(Song song) {

        int result = songListDao.updateSongMsg(song);

        if (result == 1){
            return  true;
        } else {
            return false;
        }
    }

    /**
     * 删除歌曲
     * @param id
     * @return
     */
    @Override
    public Boolean deleteSong(int id) {
        int result = songListDao.deleteSong(id);

        if (result == 1){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新歌单信息
     * @param songList
     * @return
     */
    @Override
    public ResponseData<String> updateSongListMsg(SongList songList) {

        ResponseData<String> responseData = new ResponseData<>();

        int result = songListDao.updateSongListMsg(songList);

        if (result == 1){

            responseData.setCode(1);

        } else {
            responseData.setCode(0);
        }
        return responseData;
    }

    /**
     * 删除歌单
     * @param id
     * @return
     */
    @Override
    public Boolean deleteSongList(int id) {

        int result = songListDao.deleteSongList(id);

        if (result == 1){

            return true;
        } else {
            return false;
        }
    }

    /**
     * 给歌单添加歌曲
     * @param listSong
     * @return
     */
    @Override
    public ResponseData<String> setListSong(ListSong listSong) {

        ResponseData<String> responseData = new ResponseData<>();

        ListSong listSong1 = songListDao.findListSongBySongIdAndSongListId(listSong);

        if (listSong1 == null){

            int result = songListDao.setListSong(listSong);

            if (result == 1){

                responseData.setCode(1);

                responseData.setMsg("添加成功！");
            } else {
                responseData.setCode(0);

                responseData.setMsg("添加失败！");
            }
        } else {

            responseData.setCode(0);

            responseData.setMsg("已存在");
        }
        return responseData;
    }

    /**
     * 删除歌单里的歌曲
     * @param songId
     * @return
     */
    @Override
    public Boolean deleteListSong(int songId) {

        int result = songListDao.deleteListSong(songId);

        if (result > 0){

            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加歌单
     * @param songList
     * @return
     */
    @Override
    public ResponseData<String> setSongList(SongList songList) {

        ResponseData<String> responseData = new ResponseData<>();

        songList.setPic("/img/avatorImages/默认头像.png");

        int result = songListDao.setSongList(songList);

        if (result == 1){

            responseData.setCode(1);

            responseData.setMsg("添加成功！");
        } else {
            responseData.setCode(0);

            responseData.setMsg("添加失败！");
        }
        return responseData;
    }

    @Override
    public List<Song> getAllSong() {
        return songListDao.getAllSong();
    }
}
